package com.ljh.bigevent.service.impl;

import com.ljh.bigevent.mapper.UserMapper;
import com.ljh.bigevent.pojo.Result;
import com.ljh.bigevent.pojo.User;
import com.ljh.bigevent.service.UserService;
import com.ljh.bigevent.utils.JwtUtil;
import com.ljh.bigevent.utils.Md5Util;
import com.ljh.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    ReentrantLock lock;

    @Override
    public User findByUserName(String username) {
        return userMapper.findByUserName(username);
    }

    @Override
    public Result register(String username, String password) {
        User u = userMapper.findByUserName(username);
        if(u == null) {
            String md5String = Md5Util.getMD5String(password);
            userMapper.add(username, md5String);
            return Result.success();
        } else {
            return Result.error("用户名已被占用");
        }
    }

    @Override
    public Result update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        lock.lock();
        userMapper.update(user);
        lock.unlock();
        return Result.success();
    }

    @Override
    public Result updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl, id);
        return Result.success();
    }

    @Override
    public Result updatePwd(Map<String,String> params, String token) {
        String oldPwd = params.get("old_pwd");
        String newPwd = params.get("new_pwd");
        String rePwd = params.get("re_pwd");

        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("密码不能为空");
        }

        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User loginUser = userMapper.findByUserName(username);

        if(!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))) {
            return Result.error("原密码错误");
        }

        if(!newPwd.equals(rePwd)) {
            return Result.error("两次密码不同");
        }

        userMapper.updatePwd(Md5Util.getMD5String(newPwd), loginUser.getId());

        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);
        return Result.success();
    }

    @Override
    public Result<String> login(String username, String password) {
        User loginUser = userMapper.findByUserName(username);
        if(loginUser == null) {
            return Result.error("用户名不存在");
        }
        if(Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            Map<String,Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);

            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(token, token, 12, TimeUnit.HOURS);
            return Result.success(token);
        } else {
            return Result.error("密码错误");
        }
    }

    @Override
    public Result<User> userInfo() {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userMapper.findByUserName(username);
        return Result.success(user);
    }
}